package com.niit.controller;

import com.niit.pojo.ApiResponse;
import com.niit.pojo.Brand;
import com.niit.service.BrandService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.Map;

@Controller
public class BrandController {

    @Autowired
    private BrandService brandService;

    @RequestMapping("/listBrand")
    public String selectAllBrand(Model model){

        List<Brand> brands=brandService.selectAllBrand();
        System.out.println("selectAllBrand"+brands);
        model.addAttribute("brands",brands);
        return "listBrand";
    }

    @RequestMapping("/toAddBrand")
    public String toAddBrand(){
        return "/addBrand";
    }

    @RequestMapping("/addBrand")
    public String addBrand(Brand brand) {
        System.out.println("addBrand:"+brand);
        brandService.addBrand(brand);
        return "redirect:/toAddProduct";
    }
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "/add";
    }

    @RequestMapping("/add")
    public String add(Brand brand) {
        System.out.println("addBrand:"+brand);
        brandService.addBrand(brand);
        return "redirect:/listBrand";
    }
    @RequestMapping("/toUpdateBrand")
    public String toUpdateBrand(Model model,@RequestParam("brand_id") String brand_id){
        System.out.println(brand_id);
        Brand brand=brandService.selectBrandById(Integer.parseInt(brand_id));
        System.out.println(brand);
        model.addAttribute("brand",brand);
        return "/updateBrand";
    }
    @RequestMapping("/updateBrand")
    public String updateBrand(Brand brand)  {
        brandService.updateBrand(brand);
        return "redirect:/listBrand";
    }

    @RequestMapping("/deleteBrand")
    public String deleteBrand(@RequestParam("brand_id")String brand_id){
        brandService.deleteBrand(Integer.parseInt(brand_id));
        return "redirect:/listBrand";

    }
    @RequestMapping("/toSelectBrand")
    public String toSelectBrand(@RequestParam("search") String search,Model model){
        List<Brand> brands=brandService.selectBrand(search);
        model.addAttribute("brands",brands);
        return "/selectBrand";
    }
}
